define(['app'],function(app){return {module: function($scope, $http, eventBusService, httpService, params) {

	$scope.form.father_id=params.father_id;
	$scope.form.fk_grade=params.fk_grade;
	$scope.form.fk_unit=params.fk_unit;
	if(params.father_id==undefined){
		$scope.form.show = true;
	}else{
		$scope.form.show = false;
	}
	httpService.post(app.urls.menu_select,{"id":params.father_id},function(data){
		if("0000" === data.code){
			$scope.menu_name = data.data[0].name;
			$scope.findButton(data.data[0].id);
		}else{
			alert(data.msg);
		}
	});
	$scope.findButton=function(menu_id){
		httpService.post(app.urls.button_select,{"menu_id":menu_id},function(data){
			if("0000" === data.code){
				$scope.buttons = data.data;
			}else{
				alert(data.msg);
			}
		});
	}
	//查询学年
	httpService.post(app.urls.prop_select,{"prop_group":'SCORE',"prop_key":'STU_YEAR'},function(data){
		if("0000" === data.code){
			$scope.academics =  data.data;
		}else{
			alert(data.msg);
		}
	});
	//查询学期
	httpService.post(app.urls.prop_select,{"prop_group":'SCORE',"prop_key":'STU_TERM'},function(data){
		if("0000" === data.code){
			$scope.semesters = data.data;
		}else{
			alert(data.msg);
		}
	});
	//查询主校区和分校区
	httpService.post(app.urls.unit_selectSchoolAndAttach,{},function(data){
		if("0000" === data.code){
			$scope.unitList = data.data;
		}else{
			alert(data.msg);
		}
	});
	//查询年级
	$scope.findGrade = function(){
		httpService.post(app.urls.grade_selectCenterGrade,{"fk_unit":$scope.form.fk_unit},function(data){
			if("0000" === data.code){
				$scope.gradeList = data.data;
			}else{
				alert(data.msg);
			}
		});
	}
	//获取班级
	$scope.findClass=function(){
		httpService.post(app.urls.class_selectClassName,{"fk_unit":$scope.form.fk_unit,"fk_grade":$scope.form.fk_grade,"status":'0'},function(data){
			if("0000" === data.code){
				$scope.classList = data.data;
			}else{
				alert(data.msg);
			}
		});
	}
	//查询班级数据
	$scope.find = function(){
		$scope.form.list='list';
		httpService.postpage(app.urls.class_select,$scope.form,function(data){
			if("0000" === data.code){
    			$scope.rows = data.data;
    		}else{
    			alert(data.msg);
    		}
		});
	}

	$scope.findGrade($scope.form.fk_unit);
	$scope.findClass($scope.form.fk_unit,$scope.form.fk_grade);
	$scope.find();

	//按钮点击事件-条件查询
	$scope.select = function(){
    	$scope.page.current = 1;
    	//执行查询班级数据
    	$scope.find();
    }
	//添加班主任
	$scope.addteacher = function(type,id,fk_unit){
	    eventBusService.publish("modal.page","modules/content/unitmanagement/class/addTeacher?FatherControllerName="+$scope.controllerName+"&id="+id+"&type="+type+"&fk_unit="+fk_unit);
	}
	$scope.clickBtn=function(event) {
		if (event == 'add') {
			eventBusService.publish("modal.page","modules/content/unitmanagement/class/add?FatherControllerName="+$scope.controllerName+"&father_id="+params.father_id+"&fk_grade="+$scope.form.fk_grade+"&fk_unit="+$scope.form.fk_unit);
		}
		if (event == 'update') {
			var values = [];
			$('#'+$scope.controllerName+' input[name="dataPk"]:checked').each(function(){
				values.push($(this).val());
			});
			if(values.length < 1){
				eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择修改数据。"});
			}else if(values.length > 1){
				eventBusService.publish('modal.alert', {"title":"操作提示","content":"您选择了多行数据，请重新选择。"});
			}else{
				eventBusService.publish("modal.page","modules/content/unitmanagement/class/update?FatherControllerName="+$scope.controllerName+"&father_id="+params.father_id+"&id="+values[0] +"&fk_unit="+$scope.form.fk_unit);
			}
		}
		if (event == 'delete') {
			var values = [];
			var ids="";
			$('#'+$scope.controllerName+' input[name="dataPk"]:checked').each(function(){
				var arr=$(this).val().split(',');
				values.push(arr[0]);
				ids = ids+",'"+arr[0]+"'";
			});

			if(values.length < 1){
				eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择您要删除数据。"});
			}else{
				eventBusService.publish("modal.alert",{"content":"你是否确认删除这"+values.length+"条数据？","isAutoClose":false,"confirm":function(){
						httpService.post(app.urls.class_delete,{"id":ids.replace(',','')},function(data){
							if("0000" === data.code){
								httpService.post(app.urls.class_deleteClassTeacher,{"id":ids.replace(',','')},function(data){
									if("0000" === data.code){
										httpService.post(app.urls.class_deleteClassProp,{"id":ids.replace(',','')},function(data){
											if("0000" === data.code){
												$scope.select();
												$scope.findClass($scope.form.fk_unit,$scope.form.fk_grade)
											}else{
												alert(data.msg);
											}
										});
									}else{
										alert(data.msg);
									}
								});
							}else{
								alert(data.msg);
							}
						});
					},"cancel":function(){
						//用户取消，不做操作
					}});
			}
		}
		if(event=='export'){
			var values = [];
			$('#'+$scope.controllerName+' input[name="dataPk"]:checked').each(function(){
				values.push($(this).val());
			});
			$scope.form.list = JSON.stringify(values);
			httpService.post(app.urls.class_export,$scope.form, function(data) {
				if ("0000" === data.code) {
					window.location.href = data.data;
				} else {
					alert(data.msg);
				}
			});
		}
		if(event=='template'){
			httpService.post(app.urls.class_template,{}, function(data) {
				if ("0000" === data.code) {
					window.location.href = data.data;
				} else {
					alert(data.msg);
				}
			});
		}
		if(event=='import'){
			eventBusService.publish("modal.page","modules/content/unitmanagement/class/import");
		}
		if(event=='updateSemester'){
			eventBusService.publish("modal.alert",{"content":"是否更新学年学期？","isAutoClose":false,"confirm":function(){
					httpService.post(app.urls.findYearTerm,{}, function(data) {
						if ("0000" === data.code) {
							let yearTerm = data.data;
							let yearCnt = yearTerm.reduce((counter, {type}) => type === 'STU_YEAR' ? counter += 1 : counter, 0);
							let termCnt = yearTerm.reduce((counter, {type}) => type === 'STU_TERM' ? counter += 1 : counter, 0);
							if (yearCnt != 1){
								eventBusService.publish('modal.alert', {"title":"操作提示","content":"无法更新，您设置了一个学年!"});
							}
							if (termCnt != 1){
								eventBusService.publish('modal.alert', {"title":"操作提示","content":"无法更新，您设置了一个学期!"});
							}
							if (yearCnt === 1 && termCnt === 1){
								let query = {
									'fk_year': yearTerm[0].id,
									'fk_term': yearTerm[1].id,
								}
								httpService.post(app.urls.updateClassYearTerm, query,function(data){
									if("0000" === data.code){
										eventBusService.publish("modal.alert",{"content":"更新成功！"});
									}else{
										alert(data.msg);
									}
								});
							}
						} else {
							alert(data.msg);
						}
					});
				},"cancel":function(){
					//用户取消，不做操作
				}});
		}
	}
	//接收数据刷新
	eventBusService.subscribe('content.page.reload', function(event,data) {
		$scope.select();
		$scope.findClass($scope.form.fk_unit,$scope.form.fk_grade);
	});

	$scope.showul=function(num){
		$(".gzdw").removeClass("ss-display");
	}
	
	$scope.getSchool=function(school,num){
		if(school == 0){
			$scope.form.fk_unit = "";
			$scope.gzdwname = "";
			$(".gzdw").addClass("ss-display");
		}else{
			$scope.form.fk_unit = school.id;
			$scope.gzdwname = school.name;
			$(".gzdw").addClass("ss-display");
		}
		$scope.findGrade();
	}
		//选择数据
		$scope.optData = function (element) {
			if($(element).find("input[name='dataPk']").is(":checked")){
				$(element).find("input[name='dataPk']").prop("checked",false);
				$(element).removeClass("opt_tr");
			}else{
				$(element).find("input[name='dataPk']").prop("checked",true);
				$(element).addClass("opt_tr");
			}
		}
		$scope.checkAll = function(target) {
			$("#" + $scope.controllerName + " input[name='dataPk']").each(function() {
				if ($(this).prop("checked")) {
					$(this).prop('checked', false);
					$(this).parents("tr").removeClass("opt_tr");
				} else {
					$(this).prop('checked', true);
					$(this).parents("tr").addClass("opt_tr");
				}
			})
		}
}}});